package com.ohxwj.modules.web.controller;

import com.ohxwj.common.utils.PageUtils;
import com.ohxwj.common.utils.PropertiesUtils;
import com.ohxwj.common.utils.R;
import com.ohxwj.modules.sys.entity.SysUserTokenEntity;
import com.ohxwj.modules.web.entity.WebArticleEntity;
import com.ohxwj.modules.web.entity.WebLabelEntity;
import com.ohxwj.modules.web.entity.WebTeacherVo;
import com.ohxwj.modules.web.service.WebArticleService;
import com.ohxwj.modules.web.service.WebDayHitService;
import com.ohxwj.modules.web.service.WebLabelService;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * Created by Administrator on 2018-04-18.
 */
@RestController
public class WebMaintainController extends AbstractAction{

    @Autowired
    private WebDayHitService webDayHitService;
    @RequestMapping("/website/statistics")
    public R statistics() {
        int count = webDayHitService.statisticsToday();
        int totalCount = webDayHitService.statistics();
        R r = R.ok().put("count", count);
        r.put("totalCount", totalCount);
        return r;
    }

    @RequestMapping("/website/hit")
    public R hit() {
        webDayHitService.save();
        return R.ok();
    }
    @RequestMapping("/web/maintain")
    public R maintain(String flag) {

        String newFilePath =  this.getClass().getClassLoader().getResource("").getPath() + "/views/modules/website/maintain/maintain.js";
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(newFilePath, "UTF-8");
            writer.println("var maintainFlag = " + flag);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

    @RequestMapping("/website/bannerTime")
    public R bannerTime(int time) {

        String newFilePath =  this.getClass().getClassLoader().getResource("").getPath() + "/views/modules/website/maintain/bannerTime.js";
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(newFilePath, "UTF-8");
            writer.println("var bannertime = " + time);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

    @Autowired
    private WebArticleService webArticleService;

    @RequestMapping("/website/search")
    public R search(HttpServletRequest request) {
        Configuration config = PropertiesUtils.getConfig();
        int menuId = config.getInt("newsMenuId");
        int page = getIntegerParamFromRequest(request, "page");
        int pageSize = getIntegerParamFromRequest(request, "pageSize");
        String keys = getStringParamFromRequest(request, "keys");
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", pageSize);
        params.put("offset", (page - 1) * pageSize);
        params.put("key", keys);
        params.put("menuId", menuId);
        List<WebArticleEntity> webArticleEntities = webArticleService.searchArticles(params);
        int total = webArticleService.searchTotal(params);
        PageUtils pageUtils = new PageUtils(webArticleEntities, total, pageSize, page);
        return R.ok().put("data", pageUtils);
    }

    @Autowired
    private WebLabelService webLabelService;
    @RequestMapping("/website/teacherList")
    public R teacherList(HttpServletRequest request) {
        int menuId = getIntegerParamFromRequest(request, "menuId");
        List<WebLabelEntity> webLabelEntities = webLabelService.queryLabels(menuId);
        if (null == webLabelEntities || webLabelEntities.size() == 0) {
            return R.ok();
        }
        List<WebTeacherVo> webTeacherVos = new ArrayList<>();
        for (WebLabelEntity webLabelEntity : webLabelEntities) {
            List<WebArticleEntity> webArticleEntities = webArticleService.queryArticleByLabelId(webLabelEntity.getId());
            if (null != webArticleEntities && webArticleEntities.size() > 0) {
                WebTeacherVo webTeacherVo = new WebTeacherVo();
                webTeacherVo.setLabelName(webLabelEntity.getLabelName());
                webTeacherVo.setLabelId(webLabelEntity.getId());
                webTeacherVo.setWebArticleEntities(webArticleEntities);
                webTeacherVos.add(webTeacherVo);
            }
        }
        return R.ok().put("data", webTeacherVos);
    }
}
